//
//  ViewController.m
//  Drawer
//
//  Created by 吴一桐 on 15/4/21.
//  Copyright (c) 2015年 Jiarui Han. All rights reserved.
//

#import "ViewController.h"
#import "RightDrawerTableViewController.h"
#import "LeftDrawerTableViewController.h"
#import "MainCollectionViewSmallLayout.h"
#import "MainCollectionViewController.h"

@interface ViewController ()

@property (nonatomic, retain) UIViewController *main;
@property (nonatomic, retain) UIView *mainView;
@property (nonatomic, retain) LeftDrawerTableViewController *leftDrawer;
@property (nonatomic, retain) RightDrawerTableViewController *rightDrawer;
@property (nonatomic, retain) UIImageView *topImage;
@property (nonatomic, retain) UIImageView *reflected;
@property (nonatomic, assign) NSInteger slide;

@end

@implementation ViewController

- (void)dealloc
{
    [_main release];
    [_mainView release];
    [_leftDrawer release];
    [_rightDrawer release];
    [_topImage release];
    [_reflected release];
    [_galleryImages release];
    
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.galleryImages = @[@"Image", @"Image1", @"Image2", @"Image3", @"Image4"];
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    CGRect rect = self.view.frame;
    //mainViewController
    _main = [[UIViewController alloc] init];
    [self addChildViewController:_main];
    self.main.view.frame = CGRectMake(0, 20, rect.size.width, rect.size.height);
    [self.view addSubview:_main.view];
    [_main release];
    
    //leftDrawerController
    _leftDrawer = [[LeftDrawerTableViewController alloc] init];
    [self addChildViewController:self.leftDrawer];
    _leftDrawer.view.frame = CGRectMake(-rect.size.width/2, 0, rect.size.width/2, rect.size.height);
    [_main.view addSubview:self.leftDrawer.view];
    [_leftDrawer release];
    
    //rightDrawerController
    _rightDrawer = [[RightDrawerTableViewController alloc] init];
    [self addChildViewController:_rightDrawer];
    _rightDrawer.view.frame = CGRectMake(rect.size.width, 0, rect.size.width/4, rect.size.height);
    [_main.view addSubview:self.rightDrawer.view];
    [_rightDrawer release];
    
    _mainView = [[UIView alloc] initWithFrame:rect];
    [_main.view addSubview:_mainView];
    _mainView.backgroundColor = [UIColor redColor];
    [_mainView release];
    
    _topImage = [[UIImageView alloc] initWithFrame:_mainView.frame];
    _reflected = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_topImage.bounds), 320, 320)];
    _topImage.userInteractionEnabled = YES;
    _reflected.userInteractionEnabled = YES;
    [_mainView addSubview:_topImage];
    [_mainView addSubview:_reflected];
    [_topImage release];
    [_reflected release];
    
    _reflected.transform = CGAffineTransformMakeScale(1.0, -1.0);
    
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.frame = _topImage.bounds;
    gradient.colors = @[(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:0.4] CGColor],
                        (id)[[UIColor colorWithWhite:0 alpha:0] CGColor]];
    [_topImage.layer insertSublayer:gradient atIndex:0];
    
    // Gradient to reflected image
    CAGradientLayer *gradientReflected = [CAGradientLayer layer];
    gradientReflected.frame = _reflected.bounds;
    gradientReflected.colors = @[(id)[[UIColor colorWithRed:0 green:0 blue:0 alpha:1] CGColor],
                                 (id)[[UIColor colorWithWhite:0 alpha:0] CGColor]];
    [_reflected.layer insertSublayer:gradientReflected atIndex:0];
    
    
    // Content perfect pixel
    UIView *perfectPixelContent = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_topImage.bounds), 1)];
    perfectPixelContent.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    [_topImage addSubview:perfectPixelContent];
    [perfectPixelContent release];
    
    // Label logo
    UILabel *logo = [[UILabel alloc] initWithFrame:CGRectMake(15, 32, 290, 0)];
    logo.backgroundColor = [UIColor clearColor];
    logo.textColor = [UIColor whiteColor];
    logo.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    logo.text = @"砂砾";
    [logo sizeToFit];
    // Label Shadow
    [logo setClipsToBounds:NO];
    [logo.layer setShadowOffset:CGSizeMake(0, 0)];
    [logo.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [logo.layer setShadowRadius:1.0];
    [logo.layer setShadowOpacity:0.6];
    [_mainView addSubview:logo];
    [logo release];
    
    // Label Title
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(15, logo.frame.origin.y + CGRectGetHeight(logo.frame) + 8, 290, 0)];
    title.backgroundColor = [UIColor clearColor];
    title.textColor = [UIColor whiteColor];
    title.font = [UIFont fontWithName:@"Helvetica-Bold" size:13];
    title.text = @"Heberti Almeida";
    [title sizeToFit];
    // Label Shadow
    [title setClipsToBounds:NO];
    [title.layer setShadowOffset:CGSizeMake(0, 0)];
    [title.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [title.layer setShadowRadius:1.0];
    [title.layer setShadowOpacity:0.6];
    [_mainView addSubview:title];
    [title release];
    
    // Label SubTitle
    UILabel *subTitle = [[UILabel alloc] initWithFrame:CGRectMake(15, title.frame.origin.y + CGRectGetHeight(title.frame), 290, 0)];
    subTitle.backgroundColor = [UIColor clearColor];
    subTitle.textColor = [UIColor whiteColor];
    subTitle.font = [UIFont fontWithName:@"Helvetica" size:13];
    subTitle.text = @"Lorem ipsum dolor sit amet, consectetuer adipiscing elit";
    subTitle.lineBreakMode = NSLineBreakByWordWrapping;
    subTitle.numberOfLines = 0;
    [subTitle sizeToFit];
    
    // Label Shadow
    [subTitle setClipsToBounds:NO];
    [subTitle.layer setShadowOffset:CGSizeMake(0, 0)];
    [subTitle.layer setShadowColor:[[UIColor blackColor] CGColor]];
    [subTitle.layer setShadowRadius:1.0];
    [subTitle.layer setShadowOpacity:0.6];
    [_mainView addSubview:subTitle];
    [subTitle release];

    
    MainCollectionViewSmallLayout *layout = [[MainCollectionViewSmallLayout alloc] init];
    MainCollectionViewController *collectionView = [[MainCollectionViewController alloc] initWithCollectionViewLayout:layout];
    collectionView.view.frame = CGRectMake(0, _main.view.frame.size.height - 274, _main.view.frame.size.width, 255);
//    collectionView.collectionViewLayout = layout;
//    collectionView.view.backgroundColor = [UIColor clearColor];
    [_main addChildViewController:collectionView];
    [_mainView addSubview:collectionView.view];
    [collectionView release];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [self.view addGestureRecognizer:pan];
    [pan release];

    // First Load
    [self changeSlide];
    
    NSTimer *timer = [NSTimer timerWithTimeInterval:5.0f target:self selector:@selector(changeSlide) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    
}

#pragma mark - Change slider
- (void)changeSlide
{
    if (_fullscreen == NO && _transitioning == NO) {
        if(_slide > _galleryImages.count-1) {
            _slide = 0;
        }
        
        UIImage *toImage = [UIImage imageNamed:_galleryImages[_slide]];
        [UIView transitionWithView:_main.view
                          duration:0.6f
                           options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationCurveEaseInOut
                        animations:^{
                            _topImage.image = toImage;
                            _reflected.image = toImage;
                        } completion:nil];
        _slide++;
    }
}


#pragma mark - drawer

- (void)pan:(UIPanGestureRecognizer *)pan
{
    CGPoint point = [pan locationInView:self.view];

    if (point.y < self.view.frame.size.height - 254) {
        if (pan.state == UIGestureRecognizerStateBegan || pan.state == UIGestureRecognizerStateChanged) {
            CGPoint offSet = [pan translationInView:self.view];
            CGRect rect = _mainView.frame;
            CGRect rect1 = _leftDrawer.view.frame;
            CGRect rect2 = _rightDrawer.view.frame;
            rect.origin.x += offSet.x;
            rect1.origin.x += offSet.x;
            NSLog(@"%f", rect.origin.x);
            if (_mainView.frame.origin.x >= 0 && _mainView.frame.origin.x <= self.view.frame.size.width / 2) {
                _mainView.frame = rect;
                _leftDrawer.view.frame = rect1;
            }
            if (_mainView.frame.origin.x < 0 && _mainView.frame.origin.x >= -self.view.frame.size.width / 4) {
                _mainView.frame = rect;
                _rightDrawer.view.frame = rect2;
            }
            [pan setTranslation:CGPointZero inView:self.view];
        } else if (pan.state == UIGestureRecognizerStateEnded){
                if (_mainView.frame.origin.x > 0) {
                        BOOL isVisible = _mainView.frame.origin.x >= self.view.frame.size.width/4;
                        [self showLeftDrawer:isVisible];
                } else if (_mainView.frame.origin.x < 0){
                        BOOL isVis = _mainView.frame.origin.x <= -self.view.frame.size.width/8;
                        [self showRightDrawer:isVis];
                }
        }
    }
}

#pragma mark - leftDrawer

- (void)showLeftDrawer:(BOOL)sender
{
    CGFloat width = self.view.frame.size.width/2;
    CGRect frame = self.view.bounds;
    CGRect leftFrame = _leftDrawer.view.frame;
    if (sender == YES) {
        frame.origin.x = width;
        _mainView.frame = frame;
        
        leftFrame.origin.x = 0;
        _leftDrawer.view.frame = leftFrame;
        
    } else if (sender == NO) {
        frame.origin.x = 0;
        _mainView.frame = frame;
        
        leftFrame.origin.x = -width;
        _leftDrawer.view.frame = leftFrame;
    }
}

#pragma mark - rightDrawer

- (void)showRightDrawer:(BOOL)sender
{
    CGFloat width = self.view.frame.size.width/4;
    CGRect frame = self.view.bounds;
    CGRect rightFrame = _rightDrawer.view.frame;
    if (sender == YES) {
        frame.origin.x = -width;
        _mainView.frame = frame;
        
        rightFrame.origin.x = self.view.frame.size.width - width;
        _rightDrawer.view.frame = rightFrame;
        
    } else if (sender == NO) {
        frame.origin.x = 0;
        _mainView.frame = frame;
        
        rightFrame.origin.x = self.view.frame.size.width;
        _rightDrawer.view.frame = rightFrame;

    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
